package com.my.structuraltype.flyweight;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class FlyweightFactory {

	private HashMap map = new HashMap();
	
	Flyweight build(String data) {
		if (map.containsKey(data)) {// files.containsKey是如果此映射包含对于指定
			// 键的映射关系，则返回 true
			return (Flyweight) map.get(data);// 返回指定键所映射的值；如果对
			// 于该键来说，此映射不包含任何映射关系，则返回 null
		} else {
			Flyweight fly = new ConcreteFlyweight(data);
			map.put(data, fly);
			return fly;
		}
	}
	
	public void checkFlyweight (){
        int i=0;
        System.out.println(this + ".checkFlyweight()");
        for (Iterator it = map.entrySet().iterator();it.hasNext();){
            Map.Entry e = (Map.Entry)it.next();
            System.out.println("item " + (++i) + " : " + e.getKey());
        }
        
    }
}
